package com.oldbook.manage.service.impl;

import com.oldbook.common.utils.DateUtils;
import com.oldbook.manage.domain.Order;
import com.oldbook.manage.domain.vo.OrderVO;
import com.oldbook.manage.mapper.OrderMapper;
import com.oldbook.manage.service.IOrderService;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.util.List;

/**
 * 教材订单Service业务层处理
 * 
 * @author Exiaoke
 * @date 2025-02-17
 */
@Service
public class OrderServiceImpl implements IOrderService 
{
    @Resource
    private OrderMapper orderMapper;

    /**
     * 查询教材订单
     * 
     * @param orderId 教材订单主键
     * @return 教材订单
     */
    @Override
    public OrderVO selectOrderByOrderId(Long orderId)
    {
        return orderMapper.selectOrderByOrderId(orderId);
    }

    /**
     * 查询教材订单列表
     * 
     * @param order 教材订单
     * @return 教材订单
     */
    @Override
    public List<OrderVO> selectOrderList(OrderVO order)
    {
        return orderMapper.selectOrderList(order);
    }

    /**
     * 新增教材订单
     * 
     * @param order 教材订单
     * @return 结果
     */
    @Override
    public int insertOrder(Order order)
    {
        //设置
        order.setCreateTime(DateUtils.getNowDate());
        //应还日期
        order.setDueDate(DateUtils.addDays(DateUtils.getNowDate(), 7));

        return orderMapper.insertOrder(order);
    }

    /**
     * 修改教材订单
     * 
     * @param order 教材订单
     * @return 结果
     */
    @Override
    public int updateOrder(Order order)
    {
        order.setUpdateTime(DateUtils.getNowDate());
        return orderMapper.updateOrder(order);
    }

    /**
     * 批量删除教材订单
     * 
     * @param orderIds 需要删除的教材订单主键
     * @return 结果
     */
    @Override
    public int deleteOrderByOrderIds(Long[] orderIds)
    {
        return orderMapper.deleteOrderByOrderIds(orderIds);
    }

    /**
     * 删除教材订单信息
     * 
     * @param orderId 教材订单主键
     * @return 结果
     */
    @Override
    public int deleteOrderByOrderId(Long orderId)
    {
        return orderMapper.deleteOrderByOrderId(orderId);
    }

    /**
     * 根据学号查询订单列表
     *
     * @param studentId
     * @return
     */
    @Override
    public List<OrderVO> getOrderListByStudentId(String studentId) {

        return orderMapper.selectStudentIdOrder(studentId);
    }

    /**
     * @param orderId
     * @param status
     * @return
     */
// Service层
    @Override
    public boolean updateOrderStatus(Long orderId, String status) {
        return orderMapper.updateOrderStatus(orderId,status);
    }


    /**
     * /查询所有超期未归还的订单
     * @return
     */
    @Override
    public List<Order> getOverdueOrders(){
        return orderMapper.getOverdueOrders();
    }


    /**
     * 批量更新订单状态
     *
     * @param orderIds
     * @param status
     */
    @Override
    public void batchUpdateOrderStatus(@Param("orderIds") List<Long> orderIds, @Param("status") String status) {
        orderMapper.batchUpdateOrderStatus(orderIds,status);
    }

    @Override
    public void updateLastDeductionDate(@Param("deductionDate") LocalDate deductionDate,@Param("orderId") Long orderId) {
        orderMapper.updateLastDeductionDate(deductionDate,orderId);
    }

    /**
     * 生成订单
     *
     * @param studentId
     * @param textbookId
     * @param quantity
     */
    @Override
    public void createOrder(@Param("studentId")  String studentId,
                            @Param("textbookIds") Long textbookId,
                            @Param("quantity") Integer quantity) {
        orderMapper.createOrder(studentId,textbookId,quantity);
    }
}
